﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 费用记账处理 : Form
    {
        public 费用记账处理()
        {
            InitializeComponent();
        }
        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
        void Fill()
        {
            string m = txtzhuyuanhao.Text.Trim();
            if ( m == "")
            {
                MessageBox.Show("请输入要查询的内容！");
                txtbed.Focus();


                return;
            }

          
                string sqlstr = "Select * from [hushigongzuozhan1].[dbo].[InHospital],[hushigongzuozhan1].[dbo].[Patient],[hushigongzuozhan1].[dbo].[Warn] where  [hushigongzuozhan1].[dbo].[InHospital].HosNo = [hushigongzuozhan1].[dbo].[Patient].HosNo  and  [hushigongzuozhan1].[dbo].[Patient].HosNo = [hushigongzuozhan1].[dbo].[Warn].HosNo  and [hushigongzuozhan1].[dbo].[InHospital].HosNo='" + m + "' ";

                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {

                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    } 
                   
                   
                   

                    SqlCommand cmd = new SqlCommand(sqlstr, conn);


                 
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.Read())
                        {
                            txtbed.Text = dr["BedNo"].ToString();
                            txtname.Text = dr["PatName"].ToString();

                            txtfeibie.Text = dr["PatType"].ToString();
                            txtyue.Text = dr["HosBalance"].ToString();
                            txtleibie.Text = dr["WarType"].ToString();
                            txtbianma.Text = dr["DocNo"].ToString();
                            txtkeshi.Text = dr["HosDepartment"].ToString();

                            txtfushu.Text = dr["WarTime"].ToString();


                            dr.Close();


                        } 
                    string sql = "Select WarContent, WarDosage from [hushigongzuozhan1].[dbo].[Warn] where  [hushigongzuozhan1].[dbo].[Warn].HosNo='" + m + "' and WarnCondition='执行' ";
                    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                    DataSet ds = new DataSet();
                    da.Fill(ds); 
                    dgv2.DataSource = ds.Tables[0];
                    }
                }
            
       


         

        private void btntiqv_Click(object sender, EventArgs e)
        {
            Fill();
       

        }

        private void button_bc_Click(object sender, EventArgs e)
        {
            string connString = DBHelper.connString;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "INSERT Recipe" + "(HosNo,RecProject,RecName,RecStandard,RecUnit,RecPrice,RecNumber,RecTotal,RecSelfpay,chufanghao)" + "VALUES(@HosNo,@RecProject,@RecName,@RecStandard,@RecUnit,@RecPrice,@RecNumber,@RecTotal,@RecSelfpay,@chufanghao);";
                    cmd.Parameters.Add("@HosNo", SqlDbType.VarChar, 50, "HosNo");
                    cmd.Parameters.Add("@RecProject", SqlDbType.VarChar, 50, "RecProject");
                    cmd.Parameters.Add("@RecName", SqlDbType.VarChar, 50, "RecName");
                    cmd.Parameters.Add("@RecStandard", SqlDbType.VarChar, 50, "RecStandard");
                    cmd.Parameters.Add("@RecUnit", SqlDbType.VarChar, 50, "RecUnit");
                    cmd.Parameters.Add("@RecPrice", SqlDbType.VarChar, 50, "RecPrice");
                    cmd.Parameters.Add("@RecNumber", SqlDbType.VarChar, 50, "RecNumber");
                    cmd.Parameters.Add("@RecTotal", SqlDbType.VarChar, 50, "RecTotal");
                    cmd.Parameters.Add("@RecSelfpay", SqlDbType.VarChar, 50, "RecSelfpay");
                    cmd.Parameters.Add("@chufanghao", SqlDbType.VarChar, 50, "chufanghao");

                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
                    sqlDataAdapter.InsertCommand = cmd;

                    DataTable Recipe = (DataTable)this.dgv1.DataSource;
                    conn.Open();
                    int rowAffected = sqlDataAdapter.Update(Recipe);
                    conn.Close();
                    MessageBox.Show("增加" + rowAffected.ToString() + "行。");
                    string w = rowAffected.ToString();
                    if (w != "0")
                    {

                        button_jz.Enabled = true;
                    }
                }
                catch
                {
                    MessageBox.Show("不允许有空值");
                }



            }

            Fill3();



        }
        void Fill6()
        {

            using (SqlConnection conn3 = new SqlConnection(DBHelper.connString))
            {

                string n = txtbed.Text.Trim();
                string m = txtchufanghao.Text.Trim();

                string str = "select * from [hushigongzuozhan1].[dbo].[Recipe] where   [hushigongzuozhan1].[dbo].[Recipe]. HosNo='" + n + "'";
                SqlCommand cmd = new SqlCommand(str, conn3);
                conn3.Open();

                SqlDataReader da = cmd.ExecuteReader();

                if (da.Read())
                {


                    SqlCommand sqlCommand2 = new SqlCommand();
                    sqlCommand2.Connection = conn3;
                    sqlCommand2.CommandText =
                        "UPDATE [hushigongzuozhan1].[dbo].[Recipe] SET  RecSelfpay='1'  WHERE chufanghao='" + m + "'";

                    int jj = sqlCommand2.ExecuteNonQuery();
                }

                da.Close();
                conn3.Close();
            }
        }

        private void button_jz_Click(object sender, EventArgs e)
        {
            Fill4();

            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string t = txtzongzhi.Text.Trim();

                float c = float.Parse(t);
                string n = txtbed.Text.Trim();
                int j = 0;
                float cc = 0;
                string p = txtchufanghao.Text.Trim();


                string str2 = "select * from [hushigongzuozhan1].[dbo].[InHospital],[hushigongzuozhan1].[dbo].[Recipe]  where   [hushigongzuozhan1].[dbo].[InHospital]. HosNo=[hushigongzuozhan1].[dbo].[Recipe]. HosNo  and    [hushigongzuozhan1].[dbo].[InHospital]. HosNo='" + n + "' and [hushigongzuozhan1].[dbo].[Recipe].chufanghao ='" + p + "'  ";
                SqlCommand cmd2 = new SqlCommand(str2, conn);
                conn.Open();

                SqlDataReader sqlDataReader = cmd2.ExecuteReader();

                if (sqlDataReader.Read())
                {
                    string zz = sqlDataReader["HosTotal"].ToString();
                    string ss = sqlDataReader["HosBalance"].ToString();
                    float z = float.Parse(zz);
                    float c1 = float.Parse(ss);

                    float zj = z + c;

                    cc = c1 - c;
                    SqlCommand sqlCommand = new SqlCommand();
                    sqlCommand.Connection = conn;
                    sqlCommand.CommandText =
                        "UPDATE [hushigongzuozhan1].[dbo].[InHospital]  SET HosTotal ='" + zj + "' , HosBalance = '" + cc + "'   WHERE HosNo='" + n + "'";

                    SqlCommand sqlCommand2 = new SqlCommand();
                    sqlCommand2.Connection = conn;
                    sqlCommand2.CommandText =
                        "UPDATE [hushigongzuozhan1].[dbo].[Recipe] SET  RecSelfpay='1'  WHERE chufanghao='" + p + "'";

                    SqlCommand sqlCommand3 = new SqlCommand();
                    sqlCommand3.Connection = conn;
                    sqlCommand3.CommandText =
                        "INSERT INTO  [hushigongzuozhan1].[dbo].[feiyong]( HosNo,xiangmuName,xiangmuGuige,shuliang,danjia,totalPrice,selypay) select  HosNo,RecName,RecStandard,RecNumber,RecPrice,RecTotal,RecSelfpay FROM [hushigongzuozhan1].[dbo].[Recipe] WHERE [hushigongzuozhan1].[dbo].[Recipe]. chufanghao='" + p + "' and [hushigongzuozhan1].[dbo].[Recipe].HosNo = '" + n + "' ";



                    sqlDataReader.Close();
                    txtyue.Text = Convert.ToString(cc);
                    j = sqlCommand.ExecuteNonQuery();



                    if (j > 0)
                    {
                        MessageBox.Show("余额已更新!");

                    }
                    else
                    {
                        MessageBox.Show("余额更新失败!");
                    }

                    if (cc > 0)
                    {

                        MessageBox.Show("余额足够支付,该处方已存在,但不记帐!");
                        int m = Convert.ToInt32(sqlCommand2.ExecuteNonQuery());

                    }
                    int s = sqlCommand3.ExecuteNonQuery();

                    conn.Close();
                    button_jz.Enabled = false;



                }

            }


        }

        private void dgv1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string sql = string.Format("select count(*) as hangshu from [hushigongzuozhan1].[dbo].[Recipe] where HosNo= '{0}'", txtbed.Text.Trim());
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader sqlDataReader = cmd.ExecuteReader();

                if (sqlDataReader.Read())
                {
                    string zz = sqlDataReader["hangshu"].ToString();

                    int z1 = int.Parse(zz);
                    int a = dgv1.CurrentRow.Index;




                    if (txtchufanghao.Text != "")
                    {
                        if (a > z1 - 1)
                        {


                            string w = txtbed.Text.Trim();

                            string x = txtchufanghao.Text.Trim();
                            this.dgv1.CurrentRow.Cells[0].Value = w;
                            this.dgv1.CurrentRow.Cells[9].Value = x;
                            this.dgv1.CurrentRow.Cells[8].Value = "0";
                            dgv1.CurrentRow.Cells[0].ReadOnly = true;
                            dgv1.CurrentRow.Cells[8].ReadOnly = true;
                            dgv1.CurrentRow.Cells[9].ReadOnly = true;
                        }
                        else
                        {
                            MessageBox.Show("该行为只读!");
                        }
                    }
                    else
                    {

                        MessageBox.Show("请先生成处方号!");
                    }
                }
                sqlDataReader.Close();
            }
        }

        void Fill4()
        {
            string q = txtchufanghao.Text.Trim();

            string n = txtbed.Text.Trim();




            using (SqlConnection con1 = new SqlConnection(DBHelper.connString))
            {

                string str1 = "select sum(RecTotal)as total, count(*) as jishu from [hushigongzuozhan1].[dbo].[Recipe] where chufanghao = '" + q + "' ";
                SqlCommand cmd11 = new SqlCommand(str1, con1);
                con1.Open();


                SqlDataReader da1 = cmd11.ExecuteReader();

                if (da1.Read())
                {
                    string b = da1["total"].ToString();

                    txtzongzhi.Text = b;
                }
                da1.Close();



            }



        }
        void Fill3()
        {
            SqlDataAdapter da;
            DataSet ds;


            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string sql = string.Format("select HosNo,RecProject,RecName,RecStandard,RecUnit,RecPrice,RecNumber,RecTotal,RecSelfpay,chufanghao from Recipe where HosNo= '{0}'", txtbed.Text.Trim());

                {

                    da = new SqlDataAdapter(sql, conn);
                    ds = new DataSet();
                    da.Fill(ds);
                    dgv1.DataSource = ds.Tables[0];

                }
            }
        }

        private void button_qp_Click(object sender, EventArgs e)
        {
            txtzhuyuanhao.Text = "";
            txtbed.Text = "";
            txtname.Text = "";
            txtleibie.Text = "";
            txtkeshi.Text = "";
            txtfeibie.Text = "";
            txtbianma.Text = "";
            txtyisheng.Text = "";
            txtbed.Text = "";
            txtfushu.Text = "";
            txtyue.Text = "";
            txtzhuyuankeshi.Text = "";
            label13.Text = "";
            txtchufanghao.Text = "";
            txtzongzhi.Text = "";


            dgv2.DataSource = null;
            button_jz.Enabled = false;
            button_bc.Enabled = false;
            Fill3();
        }

     
       
    }
}